package design.structure_decorator.decorators;

import java.util.Base64;

/**
 * 具体装饰器类（对字符进行base64处理）
 */
public class Base64Decorator extends DataSourceDecorator {

    public Base64Decorator(DataSource source) {
        super(source);
    }

    @Override
    public void writeData(String data) {
        //对原始字符进行base64编码
        super.writeData(Base64.getEncoder().encodeToString(data.getBytes()));
    }

    @Override
    public String readData() {
        //对base64后的字符串进行解编码
        return new String(Base64.getDecoder().decode(super.readData()));
    }

}
